package com.example.sqliteprueba;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.jjoe64.graphview.BarGraphView;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphViewDataInterface;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;

public class GraficoActivity extends Activity {
	private CargaDAO cargaDAO = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_grafico);

		// habilitar el "boton back" en la barra de title bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);

		// Inicializo las variables
		cargaDAO = new CargaDAO(this);

		// Grafico
		// Me voy 4 meses atras
		// Date referenceDate = new Date();
		// Calendar c = Calendar.getInstance();
		// c.setTime(referenceDate);
		// c.add(Calendar.MONTH, -4);

		ArrayList<GraphViewData> datos = new ArrayList<GraphViewData>();
		ArrayList<String> leyendasHorizontales = new ArrayList<String>();

		// adelanto de a 1 mes por 5 meses para incluir el mes actual.
		// for (int i = 0; i < 5; i++) {

		// leyendasHorizontales.add(format.format(c.getTime()));

		// float aux = cargaDAO.obtenerConsumoMensualPromedio(c.getTime(),6);
		int cantidadDeMeses = 3;
		int avancePorDia = 100 / cantidadDeMeses / 30;
		ArrayList<Carga> cargas = (ArrayList<Carga>) cargaDAO.obtenerCargas(
				new Date(), cantidadDeMeses);
		boolean primerTanqueLleno = false;
		float kmsBase = 0;
		float kmsSumatoria = 0;
		float litrosSumatoria = 0;
		for (int i = 0; i < cargas.size(); i++) {
			Carga carga = cargas.get(i);
			if (!primerTanqueLleno) {
				if (carga.isTanqueLleno()) {
					primerTanqueLleno = true;
					kmsBase = carga.getKm();
				}
			} else {
				if (primerTanqueLleno) { // Este if mepa que ta al cuete (probar
											// en etapa de testing ( 8-) la gran
											// posibilidad de sacarlo)
					if (carga.isTanqueLleno()) {
						kmsSumatoria += (carga.getKm() - kmsBase - kmsSumatoria);
						litrosSumatoria += carga.getLitrosCargados();
						float promedio = kmsSumatoria / litrosSumatoria;
						datos.add(new GraphViewData(i, promedio));
						SimpleDateFormat format = new SimpleDateFormat("dd-MM");
						leyendasHorizontales
								.add(format.format(carga.getFecha()));
						kmsSumatoria = 0;
						litrosSumatoria = 0;
						kmsBase = carga.getKm();
					} else {
						kmsSumatoria += (carga.getKm() - kmsBase - kmsSumatoria);
						litrosSumatoria += carga.getLitrosCargados();
					}
				}
			}
		}

		// datos.add(new GraphViewData(i + 1, aux));
		// String mesInicioS = format.format(c.getTime());
		// Log.e("fechita", format.format(c.getTime()));
		// int mesInicioI = Integer.parseInt(mesInicioS);

		// c.add(Calendar.MONTH, 1);
		// }

		// init example series data
		GraphViewData[] datos2 = new GraphViewData[datos.size()];
		datos2 = datos.toArray(datos2);
		String[] leyendasHorizontales2 = new String[leyendasHorizontales.size()];
		leyendasHorizontales2 = leyendasHorizontales
				.toArray(leyendasHorizontales2);

		GraphViewSeries exampleSeries = new GraphViewSeries(datos2);

		GraphView graphView = new LineGraphView(this // context
				, "Consumo de los Ultimo 4 Meses" // heading
		);
		graphView.addSeries(exampleSeries); // data
		graphView.setHorizontalLabels(leyendasHorizontales2);
		graphView.getGraphViewStyle().setTextSize(12);

		LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
		layout.addView(graphView);

		// // init example series data
		// GraphViewSeries exampleSeries = new GraphViewSeries(
		// new GraphViewData[] {
		// new GraphViewData(1, 2.0d),
		// new GraphViewData(2, 1.5d),
		// new GraphViewData(2.5, 3.0d) // another frequency
		// , new GraphViewData(3, 2.5d),
		// new GraphViewData(4, 1.0d) });
		//
		// GraphView graphView = new LineGraphView(this // context
		// , "GraphViewDemo" // heading
		// );
		// graphView.addSeries(exampleSeries); // data
		// graphView.setHorizontalLabels(new String[] { "10/10/14", "10/10/14",
		// "10/10/14", "10/10/14" });
		// graphView.getGraphViewStyle().setTextSize(12);
		//
		// LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
		// layout.addView(graphView);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_consumos, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		// int id = item.getItemId();
		// if (id == R.id.action_settings) {
		// return true;
		// }
		// return super.onOptionsItemSelected(item);

		switch (item.getItemId()) {
		// Respond to the action bar's Up/Home button
		case android.R.id.home:
			// Intent i = new Intent(this, MainActivity.class);
			// startActivity(i);
			// overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

			NavUtils.navigateUpFromSameTask(this);
			// overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
			overridePendingTransition(android.R.anim.fade_in,
					android.R.anim.fade_out);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
